//
//  GoodsDetailCtr-评论.swift
//  smosky
//
//  Created by zhu xietong on 2018/5/21.
//  Copyright © 2018年 tianrun. All rights reserved.
//

import Foundation
import AsyncDisplayKit
import soapp
import Eelay


class GoodsCommentListCtr:ASCollectionContainer{
    
    var scheme = SaleScheme.normal
    
    
    override func viewDidLoad() {
        //        self.page_load_section = "section1"
        self.content = ASTermsContainer.list(
            net: JoTask().post._surl(scheme.goodsCommentsListURL),
            node: "list",
            emptyMsg: "没有相关内容".loc,
            params: nil,
            cellType: GoodsDetailCtr.CommentCell.self,
            detailType: nil,
            test: false
        )
        
        super.viewDidLoad()
        self.title = "评论".loc
        paginator.parameters = ["id":mo.model["id",""]]

        collectionNode.backgroundColor = ._background
        reload_page()
        
        weak var wself = self
        
        __notice.observer(Goods.Notice.goodsOrderChange) { (noticeType, notify) in
            wself?.mo_request_send()
        }
   
    }
}



extension GoodsDetailCtr{
    
    class MoreCommentCell:JoCellNode
    {
        let bt = ASButtonNode()
        override func addLayoutRules() {
            bt.setAttributedTitle("查看所有评论".loc.ui.font13.color(color: ._foregroundWeak).center.astring, for: .normal)
            bt.contentEdgeInsets = [16,20]
            view.backgroundColor = UIColor.clear
            
            bt.addTarget(self, action: #selector(seeAllComments), forControlEvents: .touchUpInside)
        }
        
        @objc func seeAllComments() {
            let goods = DataNode.path(node: "__model", mo.model, value: NSMutableDictionary())
//            print("++ddd++++++SS|\(goods)")

            Goods.seeComments(goods: goods, at: content_controller)
        }
        
        override func specGetter() -> _Spec {
            return .stack(["v",&-.center,[0,0]], childs: [
                bt.Spec
                ])
        }
    }
    
    class EmptyCommentCell:JoCellNode
    {
        let text = JoTextNode()
        override func addLayoutRules() {
            text.attributedText = "没有相关评论".loc.ui.font13.color(color: ._foregroundWeak).center.astring
            view.backgroundColor = .clear
            
        }
        override func specGetter() -> _Spec {
            return .stack(["v",&-.center,[30,0]], childs: [
                text.Spec
                ])
        }
    }
    public class CommentImgKit: ASScanSupportKit {
    }
    
    
    public class CommentCell: ASImagesScanCell<CommentImgKit>,JWStarViewViewDelegate{
        public func starView(_ starView: JWStarView!, currentScore: CGFloat) {
            
        }
        
        
    
        let nameL = JoTextNode()
        let headV = JoImageNode()
        let timeL = JoTextNode()
        
        let contentL = JoTextNode()
        let starL = JoTextNode()
        let starV = ASDisplayNode { () -> UIView in
            let w:CGFloat = 90;let h:CGFloat = 12;
            let sv = JWStarView(frame: [0,0,w,h], numberOfStars: 5, rateStyle: .WholeStar, delegate:self)
            sv?.currentScore = 4
            sv?.lightColor = UIColor(shex:"#FFDB5D")
            sv?.backgroundColor = .clear
            return sv ?? UIView()
        }
        var starView:JWStarView?{
            get{
                return self.starV.view as? JWStarView
            }
        }

        
        
        
        override public func addLayoutRules() {
            super.addLayoutRules()
            view.backgroundColor = .clear
        }
        
        override public func mo_model_reload() {
            let list = DataNode.path(node: "images", mo.model, value: NSMutableArray())
            var imgs = [String]()
            list.list { (one:String, i) in
                imgs.append(one)
            }
            
            let imgs_list = imgs.map { (str) -> [String:Any] in
                return ["img":str]
            }
            
            imgsView.mo.model = [
                "list":imgs_list
                ].mutable_dictionary
            
            var _imgs = [EeImageResourceModel]()
            for one in imgs{
                let a = EeImageResourceModel(url: one)
                _imgs.append(a)
            }
            self.browerModels = _imgs
            
            headV.img_url = mo.model["headimgurl",""]
            headV.view.bsui.set(radius: 21, width: 1, color: UIColor(shex: "#fff3"))
            
            nameL.attributedText = mo.model["nickname",""].ui.color(color: ._foreground).font11.center.astring
            starL.attributedText = "好评".loc.ui.color(color: ._foregroundWeak).font11.left.astring
            contentL.attributedText = mo.model["content",""].ui.font12.color(color: .color(light: "#4444", dark: "ccc")).lineSpace(5).astring
            contentL.textNode.maximumNumberOfLines = 0
            
            separatorHeight = TP.separatorHeight
            separatorColor = UIColor._separator
            
            let time = stampToTime(mo.model["createtime",""])["yyyy-MM-dd HH:mm"]
            timeL.attributedText = time.ui.color(color: ._foregroundWeak).font11.left.astring
            starView?.currentScore = mo.model[float:"level",2]
            
        }
        
        override public func specGetter() -> _Spec {
            
            let left:_Spec = .stack(["v|4",50,&-.center], childs: [
                headV["42",42],
                nameL.Spec
                ])
            
            let score:_Spec = .stack([(Swidth - 42 - 16 - 2.0*AppTheme.edge),[6,0,2,0],"h|10",&-.center,_justfy(.space_between)], childs: [
                starV[90,"12"],
//                timeL["13",[2,0,0,0],_shrink(1)]
                timeL["13",[2,0,0,0]]
                ])
            let right:_Spec = .stack(["v|6",&=.stretch,_shrink(1),&-.stretch], childs: [
                score,
                contentL[_shrink(1)],
                imgsView[_shrink(1)]
                ])
            
            let spk = _Spec.stack(["h|10",[10,AppTheme.edge],_nowrap], childs: [
                left,
                right
                ])
            return spk
        }
    }
    
}

